my app/app.R

library(shiny)
library(API)
# Define UI for app that draws a histogram ----
ui <- fluidPage(

  # App title ----
  titlePanel("Hello Shiny!"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Slider for the number of bins ----
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Histogram ----
      plotOutput(outputId = "distPlot")

    )
  )
)

# Define server logic required to draw a histogram ----
server <- function(input, output) {

  # Histogram of the Old Faithful Geyser Data ----
  # with requested number of bins
  # This expression that generates a histogram is wrapped in a call
  # to renderPlot to indicate that:
  #
  # 1. It is "reactive" and therefore should be automatically
  #    re-executed when inputs (input$bins) change
  # 2. Its output type is a plot
  output$distPlot <- renderPlot({
    url <- "https://data.val.se/val/val2014/statistik/2014_landstingsval_per_kommun.xls"
    df <- API::read_data(url)
      x    <- as.numeric(df[[1]]$`S proc`)
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    hist(x, breaks = bins, col = "#75AADB", border = "red",
         xlab = "Vote Percentage's Range", ylab= "Number of kommuns",
         main = "Vote Percentage of 'Social Democratic Party'")

  })

}
shinyApp(ui = ui, server = server)
mahnazmohammadzamani/API documentation built on Dec. 21, 2021, 1:43 p.m.